package day_2022_9_to_12.Day;

/**
 * @author haomin
 * @date 2022/09/09 11:41
 **/
public class Day7_2_Num207_CanFinish {
    class Solution {
        public boolean canFinish(int numCourses, int[][] prerequisites) {
            int len = prerequisites.length;
            if(len == 0) return true;
            int[] pointer = new int[numCourses];
            for(int[] p : prerequisites) ++pointer[p[1]];
            boolean[] removed = new boolean[len];
            int remove = 0;
            while(remove < len){
                int curRemove = 0;
                for(int i = 0; i < len; i++){
                    if(removed[i]) continue;
                    int[] p = prerequisites[i];
                    if(pointer[p[0]] == 0){
                        --pointer[p[1]];
                        removed[i] = true;
                        ++curRemove;
                    }
                }
                if(curRemove == 0) return false;
                remove += curRemove;
            }
            return true;
        }
    }
}